如何在 C# 中將圖像轉換為 PDF

How to Convert Images to a PDF

This article was translated from English: Does it need improvement?
Translated
View the article in English

將圖片轉換為 PDF 是一項有用的過程,可以將多個圖像文件(如 JPG、PNG 或 TIFF)合併為一個 PDF 文檔。 這通常是為了創建數字作品集、演示文件或報告,使得共享和存儲圖片集合更加有序和通用可讀。

IronPDF 允許您將單個或多個圖像轉換為具有獨特圖像擺放和行為的 PDF。 這些行為包括適配到頁面、頁面居中以及頁面裁剪。 Additionally, you can add text and HTML headers and footers using IronPDF, apply watermarks with IronPDF, set custom page sizes, and include background and foreground overlays.

快速入門:使用 IronPDF 將圖像轉換為 PDF

使用 IronPDF 的 ImageToPdfConverter 類輕鬆將圖像轉換為 PDF 文檔。 此示例演示了如何快速將圖像文件轉換為 PDF,允許開發人員輕鬆地將圖像到 PDF 的功能集成到他們的 .NET C# 應用程序中。 使用最少的代碼,您可以將圖像轉換為 PDF,確保創建數字作品集或報告的工作流程平穩高效。

Nuget IconGet started making PDFs with NuGet now:

  1. Install IronPDF with NuGet Package Manager

    PM > Install-Package IronPdf

  2. Copy and run this code snippet.

    IronPdf.ImageToPdfConverter.ImageToPdf("path/to/image.png").SaveAs("imageToPdf.pdf");
  3. Deploy to test on your live environment

    Start using IronPDF in your project today with a free trial
    arrow pointer
class="hsg-featured-snippet">

最小工作流程(5 步)

  1. 下載用于將圖像轉換為 PDF 的 IronPDF C# 庫
  2. 準備您想要轉換的圖像
  3. ImageToPdf 靜態方法提供圖像路徑
  4. 在輸出 PDF 中調整圖像位置和行為
  5. 使用 IronPDF 添加自定義文本和 HTML 頭部和底部信息


將圖像轉換為 PDF 的示例

使用 ImageToPdfConverter 類中的 ImageToPdf 靜態方法將圖像轉換為 PDF 文檔。 此方法僅需要圖像的文件路徑,然後將其轉換為具有默認圖像位置和行為的 PDF 文檔。 支持的圖像格式包括 .bmp、.jpeg、.jpg、.gif、.png、.svg、.tif、.tiff、.webp、.apng、.avif、.cur、.dib、.ico、.jfif、.jif、.jpe、.pjp 和 .pjpeg。

示例圖像

class="content-img-align-center">
style="width=50%"> Image Sample

代码

:path=/static-assets/pdf/content-code-examples/how-to/image-to-pdf-convert-one-image.cs
using IronPdf;

string imagePath = "meetOurTeam.jpg";

// Convert an image to a PDF
PdfDocument pdf = ImageToPdfConverter.ImageToPdf(imagePath);

// Export the PDF
pdf.SaveAs("imageToPdf.pdf");
Imports IronPdf

Private imagePath As String = "meetOurTeam.jpg"

' Convert an image to a PDF
Private pdf As PdfDocument = ImageToPdfConverter.ImageToPdf(imagePath)

' Export the PDF
pdf.SaveAs("imageToPdf.pdf")
$vbLabelText   $csharpLabel

輸出PDF


將多個圖像轉換為 PDF 的示例

要將多個圖像轉換為 PDF 文檔,應提供包含文件路徑的 IEnumerable 對象,而不是單個文件路徑,就像我們前面的示例中所示。 這將再次生成具有默認圖像位置和行為的 PDF 文檔。

:path=/static-assets/pdf/content-code-examples/how-to/image-to-pdf-convert-multiple-images.cs
using IronPdf;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

// Retrieve all JPG and JPEG image paths in the 'images' folder.
IEnumerable<String> imagePaths = Directory.EnumerateFiles("images").Where(f => f.EndsWith(".jpg") || f.EndsWith(".jpeg"));

// Convert images to a PDF
PdfDocument pdf = ImageToPdfConverter.ImageToPdf(imagePaths);

// Export the PDF
pdf.SaveAs("imagesToPdf.pdf");
Imports IronPdf
Imports System
Imports System.Collections.Generic
Imports System.IO
Imports System.Linq

' Retrieve all JPG and JPEG image paths in the 'images' folder.
Private imagePaths As IEnumerable(Of String) = Directory.EnumerateFiles("images").Where(Function(f) f.EndsWith(".jpg") OrElse f.EndsWith(".jpeg"))

' Convert images to a PDF
Private pdf As PdfDocument = ImageToPdfConverter.ImageToPdf(imagePaths)

' Export the PDF
pdf.SaveAs("imagesToPdf.pdf")
$vbLabelText   $csharpLabel

輸出PDF


圖像擺放和行為

為了方便使用,我們提供了一系列有用的圖像位置和行為選項。 例如,您可以將圖像居中放置在頁面上或使其適應頁面大小,同時保持其縱橫比。 所有可用的圖像位置和行為如下:

  • TopLeftCornerOfPage: 圖像被放置在頁面的左上角。
  • TopRightCornerOfPage: 圖像被放置在頁面的右上角。
  • CenteredOnPage: 圖像居中於頁面。
  • FitToPageAndMaintainAspectRatio: 圖像適應頁面,同時保持其原始縱橫比。
  • BottomLeftCornerOfPage: 圖像被放置在頁面的左下角。
  • BottomRightCornerOfPage: 圖像被放置在頁面的右下角。
  • FitToPage: 圖像適應頁面。
  • CropPage: 頁面會調整以適應圖像。
:path=/static-assets/pdf/content-code-examples/how-to/image-to-pdf-convert-one-image-image-behavior.cs
using IronPdf;
using IronPdf.Imaging;

string imagePath = "meetOurTeam.jpg";

// Convert an image to a PDF with image behavior of centered on page
PdfDocument pdf = ImageToPdfConverter.ImageToPdf(imagePath, ImageBehavior.CenteredOnPage);

// Export the PDF
pdf.SaveAs("imageToPdf.pdf");
Imports IronPdf
Imports IronPdf.Imaging

Private imagePath As String = "meetOurTeam.jpg"

' Convert an image to a PDF with image behavior of centered on page
Private pdf As PdfDocument = ImageToPdfConverter.ImageToPdf(imagePath, ImageBehavior.CenteredOnPage)

' Export the PDF
pdf.SaveAs("imageToPdf.pdf")
$vbLabelText   $csharpLabel

圖像行為比較

class="content-img-align-center">
Place the image at the top-left of the page
class="content-img-align-center">
Place the image at the top-right of the page
class="content-img-align-center">
Place the image at the center of the page
class="content-img-align-center">
Fit the image to the page while maintaining the aspect ratio
class="content-img-align-center">
Place the image at the bottom-left of the page
class="content-img-align-center">
Place the image at the bottom-right of the page
class="content-img-align-center">
Stretch the image to fit the page
class="content-img-align-center">
Crop the page to fit the image

應用渲染選項

ImageToPdf 靜態方法下,將各種圖像轉換為 PDF 文檔的關鍵是將圖像作為 HTML <img> 標籤導入,然後將 HTML 轉換為 PDF。 這也是我們可以將 ChromePdfRenderOptions 對象作為 ImageToPdf 方法的第三個參數傳遞以直接自定義渲染過程的原因。

:path=/static-assets/pdf/content-code-examples/how-to/image-to-pdf-convert-one-image-rendering-options.cs
using IronPdf;

string imagePath = "meetOurTeam.jpg";

ChromePdfRenderOptions options = new ChromePdfRenderOptions()
{
    HtmlHeader = new HtmlHeaderFooter()
    {
        HtmlFragment = "<h1 style='color: #2a95d5;'>Content Header</h1>",
        DrawDividerLine = true,
    },
};

// Convert an image to a PDF with custom header
PdfDocument pdf = ImageToPdfConverter.ImageToPdf(imagePath, options: options);

// Export the PDF
pdf.SaveAs("imageToPdfWithHeader.pdf");
Imports IronPdf

Private imagePath As String = "meetOurTeam.jpg"

Private options As New ChromePdfRenderOptions() With {
	.HtmlHeader = New HtmlHeaderFooter() With {
		.HtmlFragment = "<h1 style='color: #2a95d5;'>Content Header</h1>",
		.DrawDividerLine = True
	}
}

' Convert an image to a PDF with custom header
Private pdf As PdfDocument = ImageToPdfConverter.ImageToPdf(imagePath, options:= options)

' Export the PDF
pdf.SaveAs("imageToPdfWithHeader.pdf")
$vbLabelText   $csharpLabel

輸出PDF

如果您想將 PDF 文檔轉換為圖像或光柵化,請參閱我們的將 PDF 光柵化為圖像指南

準備看看您還能做哪些其他事情嗎? 在這裡查看我們的教程頁面:轉換PDF

常見問題解答

如何在.NET C#中將影像轉換為PDF?

在 .NET C# 中,您可以使用 IronPDF 庫的ImageToPdfConverter類別將影像轉換為 PDF。這類提供了一種簡單的方法,只需指定影像路徑即可將影像檔案轉換為 PDF 文件。

可以使用 .NET C# 將哪些影像格式轉換為 PDF?

IronPDF支援多種影像格式轉換為PDF,包括BMP、JPEG、GIF、PNG、SVG、TIFF、WEBP等。這種靈活性確保了與大多數圖像檔案的兼容性。

如何在 C# 中將多張圖片合併成一個 PDF 文件?

要使用 IronPDF 將多張圖片合併成一個 PDF 文檔,您可以提供一個包含圖片文件路徑的IEnumerable物件。這樣就可以將所有圖片合併成一個完整的 PDF 檔案。

在PDF檔案中插入圖片有哪些選項?

IronPDF 為 PDF 中的圖像提供了多種放置選項,例如頁面左上角、頁面居中、適應頁面並保持寬高比等。這些選項可讓您自訂圖像在 PDF 頁面上的顯示方式。

轉換影像時,可以在 PDF 檔案中新增頁首和頁尾嗎?

是的,使用 IronPDF 轉換圖像時,您可以為 PDF 添加自訂文字和 HTML 頁首頁腳。此功能可用於為 PDF 文件添加額外資訊或品牌識別。

在影像轉換過程中,是否可以在 PDF 檔案中新增浮水印?

是的,IronPDF 允許您在轉換過程中為 PDF 新增浮水印。您可以按照 IronPDF 教學中的浮水印新增指南進行操作。

如何自訂影像轉PDF的轉換過程?

您可以使用 IronPDF 中的ChromePdfRenderOptions物件自訂影像到 PDF 的轉換過程。這樣,您可以調整渲染設定以滿足您的特定需求。

IronPDF 使用什麼方法將影像轉換為 PDF?

IronPDF 將圖像作為 HTML <img>標籤匯入,然後將其渲染成 PDF 格式,從而將圖像轉換為 PDF 文件。這種方法可以靈活地自訂輸出檔案。

在哪裡可以找到更多關於使用 IronPDF 將影像轉換為 PDF 的資源?

有關使用 IronPDF 將圖像轉換為 PDF 的更多資源和教程,可以在 IronPDF 官方網站及其全面的文件中找到。

將影像轉換為 PDF 格式對數位專案有何益處?

將影像轉換為 PDF 格式,可以將多張影像整理成一個可共享的文檔,從而有利於數位專案的發展。這對於創建需要以通用格式分發的數位作品集、簡報或報告來說非常理想。

IronPDF 將影像轉換為 PDF 時是否與 .NET 10 完全相容?

是的,IronPDF 完全相容於 .NET 10。該程式庫官方支援 .NET 10(以及 .NET 9、8、7、6 和 Core 版本),無需任何變通或配置即可實現影像到 PDF 的轉換、HTML 渲染以及所有關鍵功能。 ([ironpdf.com](https://ironpdf.com/?utm_source=openai))

Curtis Chau
技術作家

Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。

除了開發之外,Curtis 對物聯網 (IoT) 有著濃厚的興趣,探索將硬體和軟體結合的創新方式。在閒暇時間,他喜愛遊戲並構建 Discord 機器人,結合科技與創意的樂趣。

審核人

A PHP Error was encountered

Severity: Warning

Message: Illegal string offset 'name'

Filename: sections/author_component.php

Line Number: 70

Backtrace:

File: /var/www/ironpdf.com/application/views/main/sections/author_component.php
Line: 70
Function: _error_handler

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 63
Function: view

File: /var/www/ironpdf.com/application/views/products/sections/three_column_docs_page_structure.php
Line: 64
Function: main_view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 88
Function: view

File: /var/www/ironpdf.com/application/views/products/how-to/index.php
Line: 2
Function: view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 88
Function: view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 552
Function: view

File: /var/www/ironpdf.com/application/controllers/Products/Howto.php
Line: 31
Function: render_products_view

File: /var/www/ironpdf.com/index.php
Line: 292
Function: require_once

">

A PHP Error was encountered

Severity: Warning

Message: Illegal string offset 'title'

Filename: sections/author_component.php

Line Number: 84

Backtrace:

File: /var/www/ironpdf.com/application/views/main/sections/author_component.php
Line: 84
Function: _error_handler

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 63
Function: view

File: /var/www/ironpdf.com/application/views/products/sections/three_column_docs_page_structure.php
Line: 64
Function: main_view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 88
Function: view

File: /var/www/ironpdf.com/application/views/products/how-to/index.php
Line: 2
Function: view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 88
Function: view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 552
Function: view

File: /var/www/ironpdf.com/application/controllers/Products/Howto.php
Line: 31
Function: render_products_view

File: /var/www/ironpdf.com/index.php
Line: 292
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: Illegal string offset 'comment'

Filename: sections/author_component.php

Line Number: 85

Backtrace:

File: /var/www/ironpdf.com/application/views/main/sections/author_component.php
Line: 85
Function: _error_handler

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 63
Function: view

File: /var/www/ironpdf.com/application/views/products/sections/three_column_docs_page_structure.php
Line: 64
Function: main_view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 88
Function: view

File: /var/www/ironpdf.com/application/views/products/how-to/index.php
Line: 2
Function: view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 88
Function: view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 552
Function: view

File: /var/www/ironpdf.com/application/controllers/Products/Howto.php
Line: 31
Function: render_products_view

File: /var/www/ironpdf.com/index.php
Line: 292
Function: require_once